package casper.alpha;

/*
 * @(#)Curves.java	1.6	01/05/21
*
* Copyright 1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information").  You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.JApplet;
import javax.swing.JFrame;

import casper.alpha.utils.CasperDataGenerator;
import casper.alpha.utils.CasperUtil;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;


/**
* The Curves class illustrates CubicCurve2D & QuadCurve2D curves and includes a
* FlattenPathIterator example.
*/
public class Curves extends JApplet {

   private static Color colors[] = { Color.blue, Color.green, Color.red };
   private Geometry g, gg2, gg3, ggm, ggmr, ggmc, gb;
   private Geometry gr = null;
   private Geometry gc = null;
   
   private Geometry geo, ges;
   public Coordinate getCenter(Geometry g) {
	   Coordinate[] cs = g.getCoordinates();
	   int ccount = cs.length;
	   double x = 0;
	   double y = 0;
	   for(int i = 0; i < ccount; i++) {
		   Coordinate c = cs[i];
		   x += c.x;
		   y += c.y;
	   }
	   x /= ccount;
	   y /= ccount;
	   System.out.println("centroid " + x + "," + y);
	   return new Coordinate(x, y);
   }
   public void scale(Geometry g, double scale) {
	   Coordinate gcentroid = getCenter(g);
       for (Coordinate c : gg2.getCoordinates()) {
    	   c.x -= gcentroid.x;
    	   c.y -= gcentroid.y;
    	   c.x /= scale;
    	   c.y /= scale;
    	   c.x += gcentroid.x;
    	   c.y += gcentroid.y;
       }
   }
   public void init() {
       setBackground(Color.white);
       //g = new CasperDataGenerator().randomGeometryInGeometryNoTranslate(null);
       gb = new CasperDataGenerator().geometriesFromSeeds()[0];
       g = new CasperDataGenerator().geometriesFromSeeds()[1];
       gg2 = new CasperDataGenerator().geometriesFromSeeds()[2];
//       scale(gg2, 1.2);
       gg3 = new CasperDataGenerator().geometriesFromSeeds()[3];
       //gg2 = CasperUtil.reduceGeometry(gg2, 0.0005);
       gg3 = CasperUtil.reduceGeometry(gg3, 0.0005);
       ggm = gg2.union(gg3);
       ggmr = CasperUtil.reduceGeometry(ggm, 0.0005);
       ggmc = ggmr.convexHull();
       System.out.println("gms = " + ggm.getCoordinates().length);
       System.out.println("gmr = " + ggmr.getCoordinates().length);
       System.out.println("gmc = " + ggmc.getCoordinates().length);
       //System.out.println("gs = " + g.getCoordinates().length);
       gr = CasperUtil.reduceGeometry(g, 0.0005);
       //System.out.println("gr = " + gr.getCoordinates().length);
       gc = g.convexHull();
       //System.out.println("gc = " + gc.getCoordinates().length);
       
       geo = new CasperDataGenerator().geometriesFromSeeds()[0];
       ges = CasperUtil.reduceGeometry(geo, 0.0005);
   }
   
   boolean firstTime = true;
   
   public void drawGeometry(int w, int h, Graphics2D g2, Geometry g) {
	   Coordinate[] cs = g.getCoordinates();
	   g2.setColor(Color.black);
//	   Line2D point = new Line2D.Double(g.getCentroid().getX() - 10, 
//			   							g.getCentroid().getY() - 10, 
//			   							g.getCentroid().getX() + 10,
//			   							g.getCentroid().getY() + 10);
//	   g2.draw(point);
	   int scale = 100000;
	   int translatex = -1076900;//-2154000;//-107750 - 5276800 - 300;
	   int translatey = -10669470;//-21339300;//-1071850 - 52276400 - 300;
	   Color[] colors = new Color[]{Color.red, Color.green, Color.blue};
	   
	   int[] xpoints = new int[cs.length];
	   int[] ypoints = new int[cs.length];
	   for(int i = 0; i < cs.length - 1; i++) {
		   g2.setColor(colors[i%colors.length]);
		   
		   Line2D line = new Line2D.Double(	(cs[i].x * scale + translatex), 
				   							(cs[i].y * scale + translatey), 
				   							(cs[i+1].x * scale + translatex), 
				   							(cs[i+1].y * scale + translatey));
		   if (firstTime) {			 
			   System.out.println(line.getP1().toString() + " -- " + line.getP2().toString());
		   }
		   g2.draw(line);
//		   xpoints[i] = (int) (cs[i].x * scale + translatex);
//		   ypoints[i] = (int) (cs[i].y * scale + translatey);
//		   if (firstTime) {			 
//			   System.out.println(xpoints[i] + "," + ypoints[i]);
//		   }
	   }
	
	   java.awt.Polygon p = new java.awt.Polygon(xpoints, ypoints, cs.length);
	   g2.draw(p);
	   firstTime = false;
   }

   public void drawDemo(int w, int h, Graphics2D g2) {
	   
	   
	   /*
	   Geometry[] gs = new CasperDataGenerator().geometriesFromSeeds();
	   for(Geometry g : gs) {
		   drawGeometry(w, h, g2, g);
	   }
	   */
	   
	   g2.setColor(Color.black);
	   drawGeometry(w, h, g2, geo);
	   g2.setColor(Color.blue);
	   drawGeometry(w, h, g2, ges);
	   //drawGeometry(w, h, g2, g.convexHull());
	   g2.setColor(Color.yellow);
	   //drawGeometry(w, h, g2, gg3);
	   g2.setColor(Color.magenta);
//	   drawGeometry(w, h, g2, gg2);
   }


   public void paint(Graphics g) {
       Graphics2D g2 = (Graphics2D) g;
       Dimension d = getSize();
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                           RenderingHints.VALUE_ANTIALIAS_ON);
       g2.setBackground(getBackground());
       g2.clearRect(0, 0, d.width, d.height);
       drawDemo(d.width, d.height, g2);
   }


   public static void main(String argv[]) {
       final Curves demo = new Curves();
       demo.init();
       JFrame f = new JFrame("Java 2D Demo - Curves");
       f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}
       });
       f.getContentPane().add(demo);
       f.pack();
       f.setSize(new Dimension(800,600));
       f.setResizable(false);
       f.show();
   }
}
